package com.bawei.property

import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.bawei.common.mvi.BaseMviFragment
import com.bawei.property.adapter.ParkApplyListAdapter
import com.bawei.property.databinding.FragmentCarApplyBinding
import com.bawei.property.databinding.FragmentMineApplyBinding
import com.bawei.property.entity.ParkApplyValues
import com.bawei.property.intent.ParkApplyViewIntent
import com.bawei.property.uistate.ParkApplyUiState
import com.bawei.property.viewmodel.ParkApplyViewModel
import dagger.hilt.android.AndroidEntryPoint


/**
 * 我的车位申请
 */


@AndroidEntryPoint
class MineApplyFragment : BaseMviFragment<FragmentMineApplyBinding>() {

    private val mViewModel: ParkApplyViewModel by activityViewModels()
    private var list = mutableListOf<ParkApplyValues>()
    private lateinit var mListAdapter: ParkApplyListAdapter


    companion object {
        @JvmStatic
        fun newInstance() = MineApplyFragment()
    }

    override fun getLayoutId(): Int = R.layout.fragment_mine_apply

    override fun initViews() {
        mDataBinding.rvParkApply.layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
        mListAdapter = ParkApplyListAdapter(R.layout.item_park_apply)
        mDataBinding.rvParkApply.adapter = mListAdapter

    }

    override suspend fun sendViewIntent() {
        mViewModel.channel.send(ParkApplyViewIntent.LoadParkApplyList)
    }

    override suspend fun collectUIState() {
        mViewModel.uiState.collect{
            when(it){
                is ParkApplyUiState.LoadParkApplyListSuccess -> {
                    var data = it.data as MutableList<ParkApplyValues>
                    mListAdapter.data.addAll(data)
                    mListAdapter.notifyDataSetChanged()
                }
                is ParkApplyUiState.LoadParkApplyListFail -> showToastMessage(it.msg)
                else -> {}
            }
        }
    }

}